package logic

import (
	"context"
	"errors"
	"srv/models/mysqlinit"

	"srv/internal/svc"
	"srv/srv"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateGoodsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCreateGoodsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateGoodsLogic {
	return &CreateGoodsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CreateGoodsLogic) CreateGoods(in *srv.CreateGoodsRequest) (*srv.CreateGoodsResponse, error) {
	// todo: add your logic here and delete this line
	g := mysqlinit.Goods{}
	g = mysqlinit.Goods{
		GoodsTitle: in.GoodsTitle,
		GoodsPrice: float64(in.GoodsPrice),
		GoodsNum:   int(in.GoodsNum),
	}

	err := g.CreateGoods()
	if err != nil {
		logx.Error("商品添加失败")
		return nil, errors.New("商品添加失败")
	}

	return &srv.CreateGoodsResponse{
		Id: int64(g.ID),
	}, nil
}
